from flask import Blueprint,request,jsonify
bp_course = Blueprint('course/',__name__)
from db import db1
import json
import os


#导入文件操作配置文件
from utils.myfile import myfile


#接口
@bp_course.route('couponM/',methods=['POST','GET','PUT','DETELE'])
def couponM():
    #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
       #构造sql语句
        sql = "insert into coupon values(0,'%s',%d,'%s','%s')"%(data['name'],int(data['total']),data['stime'],data['etime'])
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    #获取一条数据
    elif request.method =='GET':
        id = request.args.get('id')
        sql = 'select * from coupon where id=%d'%(int(id))
        res = db1.find(sql)
        return jsonfiy({'code':200,'coupon':res})
    #修改数据
    elif request.method == 'PUT':
        #修改接口
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
        print(data)
        #构造sql语句
        sql = 'update coupon set name="%s",total=%d,starttime="%s",endtime="%s" where id=%d'%(data['name'],int(data['total']),data['stime'],data['etime'],int(data['id']))
        print(sql)
        db1.insert(sql)
        return jsonify({'code':200})
    #删除接口
    elif request.method =='DELETE':
        #获取id
        id = request.args.get('id')
        sql = 'delete from coupon where id=%d'%(int(id))
        db1.insert(sql)
        return jsonfy({'code':200})

#部门表增删改查
@bp_course.route('/sectorM',methods=['POST','GET','PUT','DETELE'])
def sectorM():
    #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        #构造sql语句
        sql = 'insert into sector values(0,"%s","%s")'%(data['name'],int(data['number']))
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    #获取一条数据
    elif request.method == 'GET':
        #获取id
        id = request.args.get('id')
        sql = 'select * from sector where id=%d'%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'coupon':res})
    #修改数据
    elif request.method == 'PUT':
        #获取用户输入
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        print(data)
        #构造sql语句
        sql = 'update sector set name="%s",number=%d where id=%d'%(data['name'],int(data['number']),int(data['id']))
        print(sql)
        db1.insert(sql)
        return jsonify({'code':200})
    #删除接口
    elif request.method =='DELETE':
        #获取id
        id = request.args.get('id')
        sql = 'delete from coupon where id=%d'%(int(id))
        db1.insert(sql)
        return jsonfy({'code':200})


# #文件上传
# from utils.myfile import myfile
# import os
# @bp_course.route('/upload',methods=['POST'])
# def upload():
#     #获取文件
#     file = request.files['file']
#     #选择文件夹
#     #创建文件夹
#     myfile.file_mkdir(2)
#     #保存文件
#     file.save(os.path.join('./static/upload/2',file.filename))
#     #保存
#     return 'ok'

#景点操作接口
@bp_course.route('/scenicm',methods=['POST','GET'])
def scenicm():
    if request.method == 'POST':
        #图片上传，VUE封装formdata
        data = request.form
        #添加景点
        sql = "insert into scenic(title,price,typeid)values('%s',%.2f,%d)"%(data['title'],float(data['price']),int(data['typeid']))
        db1.insert(sql)
        #获取新加的id
        sql1 = "select id from scenic where title='%s'"%(data['title'])
        res = db1.find(sql1)
        #文件上传，上传成功后图片的路径
        file = request.files['file']
        myfile.file_mkdir(str(res['id']))
        file.save(os.path.join('./static/upload/'+str(res['id']),file.filename))
        #组装数据添加
        sql3 = 'update scenic set pic="%s" where id=%d'%('/static/upload/'+str(res['id'])+"/"+file.filename,int(res['id']))
        db1.insert(sql3)
        return jsonify({'code':200})
    else:
        sql = 'select * from scenic'
        res = db1.findall(sql)
        return jsonify({'code':200,'list':res})


@bp_course.route('/city',methods=['POST','GET'])
def city():
     #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
       #构造sql语句
        sql = "insert into city values(0,'%s')"%(data['name'])
        #pymysql
        db1.insert(sql)
        #返回结果
        return 'ok'
    else:
        pass

#添加商品接口
@bp_7mcourse.route('/prom',methods=['POST'])
def prom():
     #添加
    if request.method == 'POST':
        #获取用户输入
        data = request.data
        if data:
            data=json.loads(data)
        else:
            data = request.form
    sql = "insert into product(title,price,city_id,pic) values('%s',%.2f,%d,'%s')"%(data['title'],float(data['price']),int(data['city_id']),data['pic'])
    db1.insert(sql)
    return jsonify({'code':200})

#查询所有景区
@bp_course.route('/citym')
def citym():
    sql = "select id,name from city"
    件上传接口
@bp_course.route('/upload',methods=['POST'])
res = db1.findall(sql)
    return jsonify({'code':200,'list':res})
#文def upload():
    file = request.files['file']
    cityid = request.form['cityid']
    #创建文件夹
    myfile.file_mkdir(cityid)
    #图片上传
    file.save(os.path.join('./static/upload/'+str(cityid),file.filename))
    return jsonify({'code':200,'url':'/static/upload/'+str(cityid)+'/'+file.filename})
    